#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_STRING_LENGTH 100

struct WirelessNetwork {
    int id;
    char name[MAX_STRING_LENGTH];
    char mac[MAX_STRING_LENGTH];
    char security[MAX_STRING_LENGTH];
    int signal_strength;
    char standards[MAX_STRING_LENGTH];
    char location[MAX_STRING_LENGTH];
    char internet_access[MAX_STRING_LENGTH];
    char is_active[MAX_STRING_LENGTH];
};

int main() {
    FILE *fp = fopen("recourse/APlist.txt", "r");
    if (fp == NULL) {
        printf("Error opening file!\n");
        return -1;
    }

    struct WirelessNetwork APs[MAX_STRING_LENGTH];
    char line[MAX_STRING_LENGTH*2];

    int i = 0;
    while (fgets(line, sizeof(line), fp) && i < MAX_STRING_LENGTH) {


        char *token = strtok(line, " "); // Split the line by spaces

        if (token != NULL) {
            sscanf(token, "%d", &APs[i].id); // Read the first token as an integer

            // Read other tokens with strtok
            int count = 0;
            while (token != NULL && count < 8) {
                token = strtok(NULL, " "); // Move to the next token
                if (token != NULL) {
                    switch (count) {
                        case 0: strcpy(APs[i].name, token); break;
                        case 1: strcpy(APs[i].mac, token); break;
                        case 2: strcpy(APs[i].security, token); break;
                        case 3: sscanf(token, "%d", &APs[i].signal_strength); break;
                        case 4: strcpy(APs[i].standards, token); break;
                        case 5: strcpy(APs[i].location, token); break;
                        case 6: strcpy(APs[i].internet_access, token); break;
                        case 7: strcpy(APs[i].is_active, token); break;
                    }
                    count++;
                }
            }

            if (count == 8) { // If all fields were read successfully
                i++;
            } else {
                printf("Error reading line %d\n", i + 1);
            }
        }
    }

    // Print stored data
    for (int j = 0; j < i; j++) {
        printf("AP %d: %d %s %s %s %d %s %s %s\n", j + 1, APs[j].id, APs[j].name, APs[j].mac, APs[j].security,
               APs[j].signal_strength, APs[j].standards, APs[j].location, APs[j].internet_access, APs[j].is_active);
    }

    fclose(fp);
    return 0;
}
